﻿// CAutoStartDlg.cpp: 实现文件
//

#include "stdafx.h"
#include "DC-QC.h"
#include "CAutoStartDlg.h"
#include "afxdialogex.h"

#define STABTIME 5		//5分钟的稳定时间

// CAutoStartDlg 对话框

IMPLEMENT_DYNAMIC(CAutoStartDlg, CDialogEx)

CAutoStartDlg::CAutoStartDlg(CRealTimeView* pParent )
	: CDialogEx(IDD_AUTOSTARTDLG, pParent)
{
	m_bHaveFired = FALSE;
	m_nAutoStartTimer = 0;
	m_pParent = pParent;
}

CAutoStartDlg::~CAutoStartDlg()
{
}

void CAutoStartDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_PROGRESS1, m_Progress);
}


BEGIN_MESSAGE_MAP(CAutoStartDlg, CDialogEx)
	ON_WM_TIMER()
	ON_WM_CLOSE()
	ON_NOTIFY(NM_CUSTOMDRAW, IDC_PROGRESS1, &CAutoStartDlg::OnNMCustomdrawProgress1)
	ON_BN_CLICKED(IDC_BTN_CLOSE, &CAutoStartDlg::OnBnClickedBtnClose)
END_MESSAGE_MAP()


// CAutoStartDlg 消息处理程序

BOOL CAutoStartDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化
	m_Progress.SetRange(0, 100);
	m_Progress.SetStep(1);
	m_Progress.SetPos(0);

	m_nAutoStartTimer = SetTimer(9120, 1000, NULL);
	m_nTotalSec = 0;
	m_nHeaterSec = 0;
	GetDlgItem(IDC_STARTINFO)->SetWindowTextW(_T(""));
	GetDlgItem(IDC_TESTINFO)->SetWindowTextW(_T(""));
	return TRUE;  // return TRUE unless you set the focus to a control
	// 异常: OCX 属性页应返回 FALSE
}


BOOL CAutoStartDlg::DestroyWindow()
{
	// TODO: 在此添加专用代码和/或调用基类

	return CDialogEx::DestroyWindow();
}

void CAutoStartDlg::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	if (m_nAutoStartTimer == nIDEvent)
	{
		m_nTotalSec++;
		if (m_nTotalSec == 5)		//自动连接主板
		{
			;// m_pParent->ConnectMachine();
		}
		else if (m_nTotalSec < 10)
		{
			if (gl.m_bConnChrom && m_nTotalSec == 8)
			{
				GetDlgItem(IDC_STARTINFO)->SetWindowTextW(_T("成功连接色谱核心控制板"));
				m_Progress.SetPos(10);
				m_nHeaterSec = 0;
				//正常EPC压力--早点通气，将管路中氢气转换掉
				cControl.SetEPCPars();
			}
			else if (!gl.m_bConnChrom && m_nTotalSec >= 8)
			{
				GetDlgItem(IDC_STARTINFO)->SetWindowTextW(_T("连接色谱核心控制板失败"));
				m_Progress.SetPos(2);
				m_Progress.SetPos(100);
				KillTimer(m_nAutoStartTimer);
			}
		}
		else if (m_nTotalSec == 10 && gl.m_bConnChrom)
		{
			//发送加热指令
			GetDlgItem(IDC_STARTINFO)->SetWindowTextW(_T("仪器升温过程中"));
			m_Progress.SetPos(20);
		}
		else if (m_nTotalSec > 15 && gl.m_bConnChrom)
		{
			//达到温度后才开始记时
			if (gl.gcStatus.dColumTemp > gl.gcMachine.dColumTemp - 1)
				m_nHeaterSec++;

			if (m_nHeaterSec > (STABTIME + 0.5) * 60)
			{
				//开始分析
				m_pParent->OnBnClickedRun();
				m_Progress.SetPos(100);
				KillTimer(m_nAutoStartTimer);
				m_nAutoStartTimer = 0;
				OnOK();
			}
			else if (m_nHeaterSec > STABTIME * 60)
			{
				GetDlgItem(IDC_STARTINFO)->SetWindowTextW(_T("仪器工作条件就绪"));
				m_Progress.SetPos(90);
			}
			else if (m_nHeaterSec > 68)
			{
				if (gl.gcStatus.m_nFid3Status >0 && gl.gcStatus.m_nFid1Status>0 && gl.gcStatus.m_nFid2Status>0)
				{
					m_bHaveFired = TRUE;
					GetDlgItem(IDC_STARTINFO)->SetWindowTextW(_T("仪器稳定过程中"));
					m_Progress.SetPos(50 + m_nHeaterSec * 10 / (STABTIME * 60));
				}
				else if (!m_bHaveFired)
				{
					m_pParent->OnBnClickedRun(); 
					m_bHaveFired = TRUE;
					GetDlgItem(IDC_STARTINFO)->SetWindowTextW(_T("*****仪器点火失败！*****"));
					m_nHeaterSec--;
					KillTimer(m_nAutoStartTimer);
					m_nAutoStartTimer = 0;
				}
				if (m_bHaveFired&& m_nAutoStartTimer != 0)
				{
					m_Progress.SetPos(50 + m_nHeaterSec * 10 / (STABTIME * 60));
				}
			}
			else if (m_nHeaterSec > 15 && m_nHeaterSec % 10 == 0)
			{
				GetDlgItem(IDC_STARTINFO)->SetWindowTextW(_T("设备点火"));
				m_Progress.SetPos(30 + m_nHeaterSec);
				//点火
				if (gl.gcStatus.m_nFid1Status==0){//组份A灭火,单独点火
					std::cout << "star 组份A点火" << std::endl;
					cControl.SendFireCommand(1, gl.FiredEnabled(1));
				}
				if (gl.gcStatus.m_nFid2Status==0){//组份B灭火,单独点火
					std::cout << "star 组份B点火" << std::endl;
					cControl.SendFireCommand(2, gl.FiredEnabled(2));
				}		
				if (gl.gcStatus.m_nFid3Status==0 ){//全烃灭火,单独点火
					std::cout << "start 全烃点火" << std::endl;
					cControl.SendFireCommand(0, gl.FiredEnabled(0));
				}	
				if (gl.gcStatus.m_nFid3Status >0 && gl.gcStatus.m_nFid1Status>0 && gl.gcStatus.m_nFid2Status>0)
				{
					if (m_nHeaterSec < 66)
						m_nHeaterSec = 66;		//若点着火了，直接更改时间
				}
			}
			else if (m_nHeaterSec == 67)
			{
				//正常EPC压力
				cControl.SetEPCPars();
			}

		}
		CString strTemp;
		strTemp.Format(_T("%d--%d"), m_nTotalSec, m_nHeaterSec);
		GetDlgItem(IDC_TESTINFO)->SetWindowTextW(strTemp);
	}
	CDialogEx::OnTimer(nIDEvent);
}





void CAutoStartDlg::OnNMCustomdrawProgress1(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码
	*pResult = 0;
}


void CAutoStartDlg::CloseWindow()
{
	if (m_nAutoStartTimer != 0)
	{
		KillTimer(m_nAutoStartTimer);
		//启动分析
		m_pParent->OnBnClickedRun();
	}
	CDialogEx::OnClose();
}

void CAutoStartDlg::OnBnClickedBtnClose()
{
	// TODO: 在此添加控件通知处理程序代码
	CloseWindow();
	this->OnOK();
}
 